const Base = require('./base.js');
const crypto = require('crypto');

const shasum = crypto.createHash('sha1');
shasum.update('db3d38fe4d3a97e5');
const result = shasum.digest('hex');
const resData =[
    {
        name: '影像中心',
        poi: '影像中心',
        location: '您好，影像中心在外科楼一楼西侧',
        fid: 1,
        x: 28.1,
        y: 8,
    }, {
        name: '体检中心',
        poi: '体检中心',
        location: '您好，体检中心位于医院东北角',
        fid: 101,
        x: 214.92,
        y: 84.4,
    }, {
        name: '心电室',
        poi: '心电室',
        location: '您好，心电室位于外科楼1层中央',
        fid: 1,
        x: 50.06,
        y: 8.76,
    }, {
        name: '普外科门诊',
        poi: '普外科',
        location: '您好，普外科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 108.12,
        y: 37.86,
    }, {
        name: '呼吸科专家',
        poi: '呼吸科',
        location: '您好，呼吸科专家位于门诊楼3层中央',
        fid: 23,
        x: 73.85,
        y: 36.3,
    }, {
        name: '眼科门诊',
        poi: '眼科1诊室',
        location: '您好，眼科门诊位于门诊楼2层西侧',
        fid: 22,
        x: 32.84,
        y: 21.44,
    }, {
        name: '核磁室',
        poi: '核磁室',
        location: '您好，核磁室位于外科楼1层西侧',
        fid: 1,
        x: 14.23,
        y: 7.79,
    }, {
        name: '放射科',
        poi: '第二拍片室',
        location: '您好，放射科位于外科楼1层东侧',
        fid: 1,
        x: 58.65,
        y: 6.22,
    }, {
        name: '肝病科',
        poi: '',
        location: '您好，肝病科位于外科楼9层东侧',
        fid: 9,
        x: 57.89,
        y: 8.96,
    }, {
        name: '耳鼻喉科',
        poi: '',
        location: '您好，耳鼻喉科位于外科楼7层西侧',
        fid: 7,
        x: 15.38,
        y: 8.96,
    }, {
        name: '肿瘤科',
        poi: '',
        location: '您好，肿瘤科位于外科楼4层',
        fid: 4,
        x: 38.39,
        y: 11.39,
    }, {
        name: '功能科',
        poi: '',
        location: '您好，功能科位于门诊楼3层西侧',
        fid: 23,
        x: 36.72,
        y: 38.25,
    }, {
        name: '检验科',
        poi: '',
        location: '您好，检验科位于门诊楼3层东侧',
        fid: 23,
        x: 87.52,
        y: 32.63,
    }, {
        name: '小儿科',
        poi: '',
        location: '您好，小儿科位于门诊楼7层西侧',
        fid: 27,
        x: 98.69,
        y: 36.64,
    }, {
        name: '消化科',
        poi: '',
        location: '您好，消化科位于门诊楼4层',
        fid: 24,
        x: 53.96,
        y: 19.65,
    }, {
        name: '呼吸科',
        poi: '',
        location: '您好，呼吸科位于门诊楼3层中央',
        fid: 23,
        x: 73.85,
        y: 36.3,
    }, {
        name: '骨科门诊',
        poi: '',
        location: '您好，骨科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 105.99,
        y: 37.4,
    }, {
        name: '急诊科',
        poi: '',
        location: '您好，急诊科位于门诊楼1层东侧',
        fid: 21,
        x: 123.64,
        y: 43.94,
    }, {
        name: '肝胆科',
        poi: '',
        location: '您好，肝胆科位于外科楼3层',
        fid: 3,
        x: 29.03,
        y: 8.94,
    }, {
        name: '心电图室',
        poi: '',
        location: '您好，心电图室位于门诊楼3层西侧',
        fid: 23,
        x: 30.46,
        y: 32.12,
    }, {
        name: '内分泌科门诊',
        poi: '',
        location: '您好，内分泌科门诊位于门诊楼3层中央',
        fid: 23,
        x: 71.15,
        y: 33.13,
    }, {
        name: '干部门诊',
        poi: '',
        location: '您好，干部门诊位于门诊楼3层中央',
        fid: 23,
        x: 75.15,
        y: 33.13,
    }, {
        name: '心内科门诊',
        poi: '',
        location: '您好，心内科门诊位于门诊楼3层中央',
        fid: 23,
        x: 65.15,
        y: 33.13,
    }, {
        name: '妇产科门诊',
        poi: '',
        location: '您好，妇产科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 116.64,
        y: 34.64,
    }, {
        name: '外科专家门诊',
        poi: '',
        location: '您好，外科专家门诊位于门诊楼2层东侧',
        fid: 22,
        x: 116.87,
        y: 26.2,
    }, {
        name: '口腔科',
        poi: '',
        location: '您好，口腔科位于门诊楼2层西侧',
        fid: 22,
        x: 33.74,
        y: 35.2,
    }, {
        name: '化验室',
        poi: '',
        location: '您好，化验室位于门诊楼3层东侧',
        fid: 23,
        x: 115.51,
        y: 33.13,
    }, {
        name: '针灸科',
        poi: '',
        location: '您好，针灸科位于门诊楼2层中央',
        fid: 22,
        x: 75.02,
        y: 34.75,
    }, {
        name: '心外科',
        poi: '',
        location: '您好，心外科位于外科楼8层东侧',
        fid: 8,
        x: 75.31,
        y: 8.82,
    }, {
        name: '血管造影室',
        poi: '',
        location: '您好，血管造影室位于外科楼1层东侧',
        fid: 1,
        x: 74.11,
        y: 10.83,
    }, {
        name: '药房',
        poi: '',
        location: '您好，药房位于门诊楼1层西侧',
        fid: 21,
        x: 33,
        y: 40.5,
    }, {
        name: '心脏病研究所',
        poi: '',
        location: '您好，心脏病研究所位于外科楼8层西侧',
        fid: 8,
        x: 8.7,
        y: 8.82,
    }, {
        name: '心胸外科',
        poi: '',
        location: '您好，心胸外科位于外科楼8层东侧',
        fid: 8,
        x: 73.52,
        y: 8.82,
    }, {
        name: '心胸外科门诊',
        poi: '',
        location: '您好，心胸外科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 105.84,
        y: 34.75,
    }, {
        name: '耳鼻喉科门诊',
        poi: '',
        location: '您好，耳鼻喉科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 110.84,
        y: 34.75,
    }, {
        name: '五官科门诊',
        poi: '',
        location: '您好，五官科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 108.84,
        y: 34.75,
    }, {
        name: '五官科',
        poi: '',
        location: '您好，五官科位于外科楼7层西侧',
        fid: 7,
        x: 10.19,
        y: 9,
    }, {
        name: '骨科',
        poi: '',
        location: '您好，骨科位于外科楼7层东侧',
        fid: 7,
        x: 70.07,
        y: 9.18,
    }, {
        name: '重症医学科',
        poi: '',
        location: '您好，重症医学科位于外科楼6层东侧',
        fid: 6,
        x: 69.47,
        y: 9.06,
    }, {
        name: '神经外科',
        poi: '',
        location: '您好，神经外科位于外科楼6层西侧',
        fid: 6,
        x: 7.57,
        y: 8.94,
    }, {
        name: '肾病内科',
        poi: '',
        location: '您好，肾病内科位于外科楼5层',
        fid: 5,
        x: 38.58,
        y: 9.06,
    }, {
        name: '泌尿外科',
        poi: '',
        location: '您好，泌尿外科位于外科楼5层',
        fid: 5,
        x: 36.58,
        y: 9.06,
    }, {
        name: '泌尿外科门诊',
        poi: '',
        location: '您好，泌尿外科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 116.19,
        y: 30.25,
    }, {
        name: '血液科',
        poi: '',
        location: '您好，血液科位于外科楼4层',
        fid: 4,
        x: 38.28,
        y: 8.82,
    }, {
        name: '肿瘤科',
        poi: '',
        location: '您好，肿瘤科位于外科楼4层',
        fid: 4,
        x: 38.28,
        y: 8.82,
    }, {
        name: '普外科',
        poi: '',
        location: '您好，普外科位于外科楼3层',
        fid: 3,
        x: 25.64,
        y: 9.18,
    }, {
        name: '妇产科',
        poi: '',
        location: '您好，妇产科位于外科楼2层',
        fid: 2,
        x: 26.2,
        y: 9.06,
    }, {
        name: '内分泌科',
        poi: '',
        location: '您好，内分泌科位于门诊楼10层',
        fid: 30,
        x: 35.72,
        y: 26.63,
    }, {
        name: '老年科',
        poi: '',
        location: '您好，老年科位于门诊楼9层',
        fid: 29,
        x: 30.82,
        y: 26.94,
    }, {
        name: '干部病房',
        poi: '',
        location: '您好，干部病房位于门诊楼9层',
        fid: 29,
        x: 33.82,
        y: 26.94,
    }, {
        name: '心内二科',
        poi: '',
        location: '您好，心内二科位于门诊楼8层',
        fid: 28,
        x: 33.42,
        y: 26.71,
    }, {
        name: '神经内科',
        poi: '',
        location: '您好，神经内科位于门诊楼7层东侧',
        fid: 27,
        x: 94.75,
        y: 23.15,
    }, {
        name: '心内一科',
        poi: '',
        location: '您好，心内一科位于门诊楼6层',
        fid: 26,
        x: 33.74,
        y: 26.47,
    }, {
        name: '支气管镜检查室',
        poi: '',
        location: '您好，支气管镜检查室位于门诊楼5层',
        fid: 25,
        x: 35.74,
        y: 26.47,
    }, {
        name: '呼吸科',
        poi: '',
        location: '您好，呼吸科位于门诊楼5层',
        fid: 25,
        x: 39.43,
        y: 26.71,
    }, {
        name: '内窥镜室',
        poi: '',
        location: '您好，内窥镜室位于门诊楼4层西侧',
        fid: 24,
        x: 8.69,
        y: 22.68,
    }, {
        name: '呼吸科门诊',
        poi: '',
        location: '您好，呼吸科门诊位于门诊楼3层中央',
        fid: 23,
        x: 74.26,
        y: 32.35,
    }, {
        name: '肝病门诊',
        poi: '',
        location: '您好，肝病门诊位于门诊楼3层中央',
        fid: 23,
        x: 76.26,
        y: 32.35,
    }, {
        name: '消化科门诊',
        poi: '',
        location: '您好，消化科门诊位于门诊楼3层中央',
        fid: 23,
        x: 75.26,
        y: 32.35,
    }, {
        name: '肿瘤科门诊',
        poi: '',
        location: '您好，肿瘤科门诊位于门诊楼2层东侧',
        fid: 22,
        x: 30.82,
        y: 32.39,
    }, {
        name: '皮肤科',
        poi: '',
        location: '您好，皮肤科位于门诊楼2层东侧',
        fid: 22,
        x: 114.84,
        y: 33.97,
    }, {
        name: '胸痛中心',
        poi: '',
        location: '您好，胸痛中心位于门诊楼1层东侧',
        fid: 21,
        x: 118.84,
        y: 38.42,
    }, {
        name: '眼科',
        poi: '',
        location: '您好，眼科位于门诊楼8层东侧',
        fid: 28,
        x: 69.47,
        y: 9.18,
    }, {
        name: '眼科住院部',
        poi: '眼科住院部',
        location: '您好，眼科在院内东北角眼科中心大楼',
        fid: 101,
        x: 214.92,
        y: 84.4,
    }
];
module.exports = class extends Base {
    async targetAction() {
        const data = await this.cache('poi_name');
        this.cache('poi_name', null);
        return this.json(data || '{}');
    }

    indexAction() {
        if (this.isPost) { // 如果是 POST 请求

            const content = this.ctx.request.body.post.Msg.Content;

            const decoded = new Buffer(content, 'base64').toString();

            const decodedSessionParams = new Buffer(this.ctx.request.body.post.SessionParams, 'base64').toString();

            this.ctx.request.body.post.SessionParams = JSON.parse(decodedSessionParams);
            this.ctx.request.body.post.Msg.Content = JSON.parse(decoded);

            const preWriteString = JSON.stringify(this.ctx.request.body.post) + '\n';
            think.logger.warn('接到的AIUI请求' + preWriteString);

            let name = '';
            const intent = JSON.parse(decoded).intent;

            // console.log("intent.text = " + intent.text);
            if (intent.text !== undefined) {
                if (intent.category == 'PHYSICS.navigation_poi') {
                    name = intent.semantic[0].slots[0].value;
                    var poiJson;
                    for (var j = 0, len = resData.length; j < len; j++) {
                        if (resData[j].name === name) {
                            poiJson = resData[j];
                        }
                    }
                    var mText = '';
                    think.logger.warn('poi_name' + poiJson);
                    think.logger.warn('res_name' + name);
                    this.cache('poi_name', poiJson);
                    if (poiJson) {
                        mText = poiJson.location;
                    }
                    this.ctx.request.body.post.Msg.Content.intent.answer = {'text': mText};
                    think.logger.warn('返回结果' + JSON.stringify(this.ctx.request.body.post.Msg.Content));
                    return this.json(this.ctx.request.body.post.Msg.Content);
                }
            }
            think.logger.warn('返回结果' +JSON.stringify(this.ctx.request.body.post.Msg.Content));
            return this.json(this.ctx.request.body.post.Msg.Content);
        }
        return this.json(result);
    }
};
